using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SMSSender.Entities.Models;
using SMSSender.Entities.Utils;
using SMSSender.Common;

namespace SMSSender.DAL.Base
{
	public class MessageHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public MessageHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the MessageHistory table.
		/// </summary>
		public virtual void Insert(MessageHistoryInfo messageHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMessage", messageHistoryInfo.IdMessage),
				new SqlParameter("@PostDate", messageHistoryInfo.PostDate),
				new SqlParameter("@Sender", messageHistoryInfo.Sender),
				new SqlParameter("@Reciver", messageHistoryInfo.Reciver),
				new SqlParameter("@Conten", messageHistoryInfo.Conten),
				new SqlParameter("@Status", messageHistoryInfo.Status),
				new SqlParameter("@Flag", messageHistoryInfo.Flag)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the MessageHistory table.
		/// </summary>
		public virtual void Update(MessageHistoryInfo messageHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMessage", messageHistoryInfo.IdMessage),
				new SqlParameter("@PostDate", messageHistoryInfo.PostDate),
				new SqlParameter("@Sender", messageHistoryInfo.Sender),
				new SqlParameter("@Reciver", messageHistoryInfo.Reciver),
				new SqlParameter("@Conten", messageHistoryInfo.Conten),
				new SqlParameter("@Status", messageHistoryInfo.Status),
				new SqlParameter("@Flag", messageHistoryInfo.Flag)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the MessageHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idMessage)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMessage", idMessage)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the MessageHistory table.
		/// </summary>
		public virtual MessageHistoryInfo Select(Guid? idMessage)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMessage", idMessage)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeMessageHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        /// <summary>
        /// SelectTopOne records from the MessageHistory table.
        /// </summary>
        public virtual MessageHistoryInfo SelectTopOne()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageHistory_SelectTopOne"))
            {
                if (dataReader.Read())
                {
                    return MakeMessageHistoryInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }

		/// <summary>
		/// Selects all records from the MessageHistory table.
		/// </summary>
		public List<MessageHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageHistory_SelectAll"))
			{
				List<MessageHistoryInfo> messageHistoryInfoList = new List<MessageHistoryInfo>();
				while (dataReader.Read())
				{
					messageHistoryInfoList.Add(MakeMessageHistoryInfo(dataReader));
				}

				return messageHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the MessageHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual MessageHistoryInfo MakeMessageHistoryInfo(SqlDataReader dataReader)
		{
			MessageHistoryInfo messageHistoryInfo = new MessageHistoryInfo();
			messageHistoryInfo.IdMessage = SqlClientUtility.GetGuid(dataReader, DbConstants.MESSAGEHISTORY.IDMESSAGE);
			messageHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.MESSAGEHISTORY.POSTDATE);
			messageHistoryInfo.Sender = SqlClientUtility.GetString(dataReader, DbConstants.MESSAGEHISTORY.SENDER);
			messageHistoryInfo.Reciver = SqlClientUtility.GetString(dataReader, DbConstants.MESSAGEHISTORY.RECIVER);
			messageHistoryInfo.Conten = SqlClientUtility.GetString(dataReader, DbConstants.MESSAGEHISTORY.CONTEN);
			messageHistoryInfo.Status = SqlClientUtility.GetBoolean(dataReader, DbConstants.MESSAGEHISTORY.STATUS);
			messageHistoryInfo.Flag = SqlClientUtility.GetBoolean(dataReader, DbConstants.MESSAGEHISTORY.FLAG);

			return messageHistoryInfo;
		}

		#endregion
	}
}
